/*
 * @Descripttion:
 * @version:
 * @Author: koala
 * @Date: 2021-12-11 15:48:24
 * @LastEditors: koala
 * @LastEditTime: 2022-01-21 10:50:48
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Post,
  Put,
  Query,
  Req,
  UseGuards
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { Roles, RolesGuard } from './../auth/role.guard';
import { CreatePostDto, PostsRo, updatePostDto } from './dto/post.dto';
import { PostsService } from './posts.service';

@ApiTags('文章')
@Controller('post')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}

  /**
   * 创建文章
   */
  @ApiOperation({ summary: '创建文章' })
  @ApiBearerAuth()
  @Post()
  @Roles('admin', 'root')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  async create(@Body() post: CreatePostDto, @Req() req) {
    return await this.postsService.create(req.user, post);
  }

  /**
   * 获取所有文章
   */
  @ApiOperation({ summary: '获取文章列表' })
  @Get('/list')
  @ApiQuery({ name: 'category', required: false, type: Number })
  @ApiQuery({ name: 'author', required: false, type: String })
  @ApiQuery({ name: 'title', required: false, type: String })
  @ApiQuery({ name: 'status', required: false, type: String })
  @ApiQuery({name:'tag',required:false,type:Array})
  async findAll(
    @Query() query,
    @Query('pageSize') pageSize: number,
    @Query('pageNum') pageNum: number,
    @Query('category') category?: number,
    @Query('author') author?: string,
    @Query('title') title?: string,
    @Query('status') status?: string,
    @Query('tag') tag?: Array<number>,
  ): Promise<PostsRo> {
    return await this.postsService.findAll(query);
  }
  /**
   * 获取归档列表
   */
  @ApiOperation({ summary: '归档日期列表' })
  @Get('/archives')
  getArchives() {
    return this.postsService.getArchives();
  }

  /**
   * 获取文章归档
   */
  @ApiOperation({ summary: '文章归档' })
  @Get('/archives/list')
  getArchiveList(@Query('time') time: string) {
    return this.postsService.getArchiveList(time);
  }

  /**
   * 获取指定文章
   * @param id
   */
  @ApiOperation({ summary: '获取指定文章' })
  @Get('/read')
  async findById(@Query('id') id: number) {
    return await this.postsService.findById(id);
  }

  /**
   * 更新文章
   * @param id
   * @param post
   */
  @ApiOperation({ summary: '更新指定文章' })
  @ApiBearerAuth()
  @Put('/update')
  @UseGuards(AuthGuard('jwt'))
  async update(@Body() post: updatePostDto) {
    return await this.postsService.updateById(post);
  }

  /**
   * 删除
   * @param id
   */

  @ApiOperation({ summary: '删除文章' })
  @ApiBearerAuth()
  @Delete('/delete')
  @UseGuards(AuthGuard('jwt'))
  async remove(@Query('id') id: number) {
    return await this.postsService.remove(id);
  }

  @Get('/tags')
  async findArticles(@Query('tags') tags:Array<number>) {
    return this.postsService.findArticleByTags(tags);
  }
}
